package com.hanxin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanxin.mapper.AppealAddMapper;
import com.hanxin.mapper.AppealMapper;
import com.hanxin.pojo.Result;
import com.hanxin.pojo.TAudit;
import com.hanxin.pojo.Twoindex;

import com.hanxin.service.AppealAddService;
import com.hanxin.service.AppealService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AppealAddServiceImpl implements AppealAddService {

    @Autowired
    private AppealAddMapper appealAddMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private String hashKey;
//    @Autowired
//    private Useres useres;
    @Override
    public Result<Object> addGetAppeal() {
        Twoindex twoindex =new Twoindex();
        twoindex.setYear(2023);
        // TODO
//        String principal = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        User user = useres.userss(principal);
//        twoindex.setUnit(user.getUnit());
        twoindex.setUnit("省农业农村厅");
        twoindex.setAppealstatus("已审核");
        twoindex.setProgressstatus("已审核");
        QueryWrapper<Twoindex> queryWrapper = new QueryWrapper<>(twoindex);
        // 根据Twoindex条件查询数据库
        List<Twoindex> selected = appealAddMapper.selectList(queryWrapper);
        if (selected.size()<=0) {
            return Result.error("当前没有数据");
        }
        String value = JSON.toJSONString(selected);
        // 将查询到的数据存入redis
        String hashKey = " Level2:" + twoindex.getYear() + ":" + twoindex.getUnit() + ":" +
                twoindex.getAppealstatus() + ":" + twoindex.getProgressstatus();
        redisTemplate.opsForHash().put("Level2",hashKey,value);
        // 设置缓存过期时间为30分钟
        redisTemplate.expire("Level2",30, TimeUnit.MINUTES);
        // 判断是否有缓存，有缓存则使用缓存
        if (redisTemplate.opsForHash().hasKey("Level2",hashKey)) {
            List<Twoindex> value2 = JSON.parseArray((String) redisTemplate.opsForHash().get("Level2", hashKey), Twoindex.class);
            return Result.ok(value2);
        }
        return Result.ok(selected);

    }
}
